package com.ysc.menu;

import com.ysc.domain.Bill;
import com.ysc.domain.Cla;
import com.ysc.domain.Student;
import com.ysc.domain.Teacher;
import com.ysc.utils.mybatisUtils;
import org.apache.ibatis.session.SqlSession;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@Controller
public class HomeController {

    //查询部分
    @RequestMapping("/ShowAllTeachers")
    public static String ShowAllTeachers(HttpServletRequest request){
        SqlSession sqlSession = mybatisUtils.getSqlSession();
        String SqlID= "com.ysc.Dao.TeacherDao.selectAllTeachers";
        List<Teacher> teachers = sqlSession.selectList(SqlID);
        request.setAttribute("teachers",teachers);
        sqlSession.close();
        return "Show/ShowTea";
    }

    //展示所有学生的信息
    @RequestMapping("/ShowAllStudents")
    public static String ShowAllStudents(HttpServletRequest request){

        SqlSession sqlSession = mybatisUtils.getSqlSession();
        String SqlID= "com.ysc.Dao.StudentDao.selectAllStudents";
        List<Student> students = sqlSession.selectList(SqlID);
        request.setAttribute("students",students);
        sqlSession.close();
        return "Show/ShowStu";
    }

    //查看所有班级信息
    @RequestMapping("/ShowAllClasses")
    public static String ShowAllClasses(HttpServletRequest request){

        SqlSession sqlSession = mybatisUtils.getSqlSession();
        String SqlID= "com.ysc.Dao.ClassDao.ShowAllClasses";
        List<Cla> classes = sqlSession.selectList(SqlID);
        request.setAttribute("cla",classes);
        sqlSession.close();
        return "Show/ShowCla";
    }

    //修改部分
    @RequestMapping("/updateStu")
    public static String updateStu(HttpServletRequest request) {

        int sid = Integer.parseInt(request.getParameter("sid"));
        System.out.println(sid);
        SqlSession sqlSession = mybatisUtils.getSqlSession();
        String SqlId = "com.ysc.Dao.StudentDao.selectStudent";
        Student stu = new Student();
        stu.setStid(sid);
        Student queryStu = sqlSession.selectOne(SqlId,stu);
        request.setAttribute("student",queryStu);
        System.out.println(queryStu.toString());
        sqlSession.close();
        return "UPDATE/StudentUpdate";
    }

    @RequestMapping("/StudentUpdatePost")
    public static String StudentUpdatePost(Student student) {

        SqlSession sqlSession = mybatisUtils.getSqlSession();
        String SqlId = "com.ysc.Dao.StudentDao.updateStudent";
        int Num = sqlSession.update(SqlId,student);
        sqlSession.commit();
        sqlSession.close();
        if(Num>0){
            return "UPDATE/success";
        }
        else
        {
            return "UPDATE/failure";
        }

    }

    @RequestMapping("/updateCla")
    public static String updateCla(HttpServletRequest request) {

        int cid = Integer.parseInt(request.getParameter("cid"));
        SqlSession sqlSession = mybatisUtils.getSqlSession();
        String SqlId = "com.ysc.Dao.ClassDao.selectClass";
        Cla cla = new Cla();
        cla.setCid(cid);
        Cla querycla = sqlSession.selectOne(SqlId,cla);
        request.setAttribute("cla",querycla);
        System.out.println(querycla);
        sqlSession.close();
        return "UPDATE/ClassUpdate";
    }

    @RequestMapping("/ClassUpdatePost")
    public static String ClassUpdatePost(Cla cla) {

        SqlSession sqlSession = mybatisUtils.getSqlSession();
        String SqlId = "com.ysc.Dao.ClassDao.updateClass";
        int Num = sqlSession.update(SqlId,cla);
        sqlSession.commit();
        sqlSession.close();
        if(Num>0){
            return "UPDATE/success";
        }
        else
        {
            return "UPDATE/failure";
        }

    }

    //删除部分
    @RequestMapping("/deleteStu")
    public static String deleteStu(HttpServletRequest request) {

        SqlSession sqlSession = mybatisUtils.getSqlSession();
        int sid = Integer.parseInt(request.getParameter("sid"));
        String SqlId = "com.ysc.Dao.StudentDao.deleteStudent";
        int Num = sqlSession.delete(SqlId,sid);
        sqlSession.commit();
        sqlSession.close();
        if(Num>0){
            return "DELETE/success";
        }
        else
        {
            return "DELETE/failure";
        }
    }

    @RequestMapping("/deleteCla")
    public static String deleteCla(HttpServletRequest request) {

        SqlSession sqlSession = mybatisUtils.getSqlSession();
        int cid = Integer.parseInt(request.getParameter("cid"));
        String SqlId = "com.ysc.Dao.ClassDao.deleteClass";
        int Num = sqlSession.delete(SqlId,cid);
        sqlSession.commit();
        sqlSession.close();
        if(Num>0){
            return "DELETE/success";
        }
        else
        {
            return "DELETE/failure";
        }
    }


    //添加信息
    @RequestMapping("/StudentAdd")
    public static String StudentAdd() {

        return "ADD/StudentAdd";
    }

    @RequestMapping("/StudentAddPost")
    public static String StudentAddPost(Student student) {
        System.out.println(student.toString());
        SqlSession sqlSession = mybatisUtils.getSqlSession();

        String Sqlid = "com.ysc.Dao.StudentDao.insertStudent";
        int Num = sqlSession.insert(Sqlid,student);
        sqlSession.commit();
        sqlSession.close();
        System.out.println(Num + "个学生信息已录入，请继续填报账户信息并缴费。");
        if(Num>0)
            return "ADD/success";
        else
            return "ADD/failure";
    }



    @RequestMapping("/BillAdd")
    public static String BillAdd() {

        return "ADD/BillAdd";
    }

    @RequestMapping("/BillAddPost")
    public static String BillAddPost(Bill bill) {

        SqlSession sqlSession = mybatisUtils.getSqlSession();
        int Num;
        String Sqlid = "com.ysc.Dao.ClassDao.getCnum";
        Cla cla = new Cla();
        cla.setCid(bill.getCid());
        int cnum = sqlSession.selectOne(Sqlid,cla);

        Sqlid = "com.ysc.Dao.BillDao.checkIfinClass";
        int N = sqlSession.selectOne(Sqlid,bill);
        System.out.println(N);
        if(N == 0){
            Sqlid = "com.ysc.Dao.ClassDao.UpdateCNum";
            sqlSession.update(Sqlid,cla);
            sqlSession.commit();
        }

        if (cnum<10){
            Sqlid = "com.ysc.Dao.ClassDao.GetTidByCid";
            cla.setCid(bill.getCid());
            int tid = sqlSession.selectOne(Sqlid,cla);

            Sqlid = "com.ysc.Dao.TeacherDao.getOncePrice";
            Teacher teacher = new Teacher();
            teacher.setTid(tid);
            int price = sqlSession.selectOne(Sqlid,teacher);

            Sqlid = "com.ysc.Dao.BillDao.insertBill";
            bill.setLastClassNum(bill.getBill_Account()/price);
            Num = sqlSession.insert(Sqlid,bill);
            sqlSession.commit();

            System.out.println(Num + "个收费信息已录入");
            sqlSession.close();
            if(Num>0) {
                HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
                request.setAttribute("bill",bill);
                return "ADD/Getinvince";//打印发票
            }
            else
                return "ADD/failure";
        }
        else{
            return "ADD/fail1";
        }
    }

    @RequestMapping("/Exit")
    public static String Exit(){
        return "Quit";
    }
}
